package com.pj.project.sys_dept;

import java.util.List;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import com.pj.current.satoken.StpUserUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: sys_dept -- 部门表
 * @author xxxxxx
 */
@RestController
@RequestMapping("/SysDept/")
public class SysDeptController {

	/** 底层 Service 对象 */
	@Autowired
	SysDeptService sysDeptService;

	/** 增 */
	@RequestMapping("add")
	@SaCheckPermission(SysDept.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(SysDept s){
		sysDeptService.add(s);
		s = sysDeptService.getById(SP.publicMapper.getPrimarykey());
		return AjaxJson.getSuccessData(s);
	}

	/** 删 */
	@RequestMapping("delete")
	@SaCheckPermission(SysDept.PERMISSION_CODE)
	public AjaxJson delete(Long id){
		int line = sysDeptService.delete(id);
		return AjaxJson.getByLine(line);
	}

	/** 删 - 根据id列表 */
	@RequestMapping("deleteByIds")
	@SaCheckPermission(SysDept.PERMISSION_CODE)
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
		int line = SP.publicMapper.deleteByIds(SysDept.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}

	/** 改 */
	@RequestMapping("update")
	@SaCheckPermission(SysDept.PERMISSION_CODE)
	public AjaxJson update(SysDept s){
		int line = sysDeptService.update(s);
		return AjaxJson.getByLine(line);
	}

	/** 查 - 根据id */
	@RequestMapping("getById")
	public AjaxJson getById(Long id){
		SysDept s = sysDeptService.getById(id);
		return AjaxJson.getSuccessData(s);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	@RequestMapping("getList")
	public AjaxJson getList() {
		SoMap so = SoMap.getRequestSoMap();
		List<SysDept> list = sysDeptService.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/** 查集合 (整个表数据转化为tree结构返回) */
	@RequestMapping("getTree")
	public AjaxJson getTree() {
		// 获取记录
		SoMap so = SoMap.getRequestSoMap();
		List<SysDept> list = sysDeptService.getList(so);
		// 转为tree结构，并返回
		List<SoMap> listMap = SoMap.getSoMapByList(list);
		List<SoMap> listTree = SoMap.listToTree(listMap, "id", "parentId", "children");
		return AjaxJson.getPageData(Long.valueOf(listMap.size()), listTree);
	}



	// ------------------------- 前端接口 -------------------------


	/** 改 - 不传不改 [G] */
	@RequestMapping("updateByNotNull")
	public AjaxJson updateByNotNull(Long id){
		AjaxError.throwBy(true, "如需正常调用此接口，请删除此行代码");
		// 鉴别身份，是否为数据创建者
		long userId = SP.publicMapper.getColumnByIdToLong(SysDept.TABLE_NAME, "user_id", id);
		AjaxError.throwBy(userId != StpUserUtil.getLoginIdAsLong(), "此数据您无权限修改");
		// 开始修改 (请只保留需要修改的字段)
		SoMap so = SoMap.getRequestSoMap();
		so.clearNotIn("id", "name", "intro", "parentId", "createTime").clearNull().humpToLineCase();
		int line = SP.publicMapper.updateBySoMapById(SysDept.TABLE_NAME, so, id);
		return AjaxJson.getByLine(line);
	}







}
